<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="客户编码:">
        <el-input v-model="queryParams.customerCode" size="small" placeholder="请输入客户编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客户名称:">
        <el-input v-model="queryParams.customerName" size="small" placeholder="请输入客户名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="对账金额:">
        <el-input v-model="queryParams.amount" size="small" placeholder="请输入对账金额" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="调整金额:">
        <el-input v-model="queryParams.adjustAmount" size="small" placeholder="请输入调整金额" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="业务单据号:">
        <el-input v-model="queryParams.basicNo" size="small" placeholder="请输入业务单据号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="支付流水号:">
        <el-input v-model="queryParams.slipNo" size="small" placeholder="请输入支付流水号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属工会:">
        <el-input v-model="queryParams.unionName" size="small" placeholder="请输入所属工会" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="收货人名称:">
        <el-input v-model="queryParams.consigneeName" size="small" placeholder="请输入收货人名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="送货地址:">
        <el-input v-model="queryParams.consigneeAddress" size="small" placeholder="请输入送货地址" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="收货人电话:">
        <el-input v-model="queryParams.consgneePhone" size="small" placeholder="请输入收货人电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="订单来源:">
        <el-input v-model="queryParams.orderSource" size="small" placeholder="请输入订单来源" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="订单数量:">
        <el-input v-model="queryParams.wtCnt" size="small" placeholder="请输入订单数量" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="供应商ID:">
        <el-input v-model="queryParams.supplierNo" size="small" placeholder="请输入供应商ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="供应商名称:">
        <el-input v-model="queryParams.supplierName" size="small" placeholder="请输入供应商名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="清关时间:">
        <el-input v-model="queryParams.customClearanceDate" size="small" placeholder="请输入清关时间" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="提单人:">
        <el-input v-model="queryParams.processPersonName" size="small" placeholder="请输入提单人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="付款时间:">
        <el-input v-model="queryParams.paymentDate" size="small" placeholder="请输入付款时间" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermission="['module:apConfirm:insert']" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['module:apConfirm:update']" type="success" icon="el-icon-edit" size="mini" :disabled="isSingle" @click="handleEdit">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['module:apConfirm:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="客户编码" align="center" prop="customerCode" />
      <el-table-column label="客户名称" align="center" prop="customerName" />
      <el-table-column label="对账金额" align="center" prop="amount" />
      <el-table-column label="调整金额" align="center" prop="adjustAmount" />
      <el-table-column label="业务单据号" align="center" prop="basicNo" />
      <el-table-column label="支付流水号" align="center" prop="slipNo" />
      <el-table-column label="所属工会" align="center" prop="unionName" />
      <el-table-column label="收货人名称" align="center" prop="consigneeName" />
      <el-table-column label="送货地址" align="center" prop="consigneeAddress" />
      <el-table-column label="收货人电话" align="center" prop="consgneePhone" />
      <el-table-column label="订单来源" align="center" prop="orderSource" />
      <el-table-column label="订单数量" align="center" prop="wtCnt" />
      <el-table-column label="供应商ID" align="center" prop="supplierNo" />
      <el-table-column label="供应商名称" align="center" prop="supplierName" />
      <el-table-column label="订单状态" align="center" prop="orderStatus" />
      <el-table-column label="清关时间" align="center" prop="customClearanceDate" />
      <el-table-column label="提单人" align="center" prop="processPersonName" />
      <el-table-column label="付款时间" align="center" prop="paymentDate" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermission="['module:apConfirm:update']" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
          <el-button v-hasPermission="['module:apConfirm:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

    <!-- 添加/修改对话框 -->
    <el-dialog :title="apConfirmFormDialog.title" :visible.sync="apConfirmFormDialog.visible" :close-on-click-modal="false" :before-close="resetApConfirmFormDialog" width="500px" append-to-body>
      <el-form ref="apConfirmForm" :model="apConfirmFormData" :rules="apConfirmFormRules" label-width="80px">
        <el-form-item label="客户编码" prop="customerCode">
          <el-input v-model="apConfirmFormData.customerCode" placeholder="请输入客户编码" />
        </el-form-item>
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="apConfirmFormData.customerName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="对账金额" prop="amount">
          <el-input v-model="apConfirmFormData.amount" placeholder="请输入对账金额" />
        </el-form-item>
        <el-form-item label="调整金额" prop="adjustAmount">
          <el-input v-model="apConfirmFormData.adjustAmount" placeholder="请输入调整金额" />
        </el-form-item>
        <el-form-item label="业务单据号" prop="basicNo">
          <el-input v-model="apConfirmFormData.basicNo" placeholder="请输入业务单据号" />
        </el-form-item>
        <el-form-item label="支付流水号" prop="slipNo">
          <el-input v-model="apConfirmFormData.slipNo" placeholder="请输入支付流水号" />
        </el-form-item>
        <el-form-item label="所属工会" prop="unionName">
          <el-input v-model="apConfirmFormData.unionName" placeholder="请输入所属工会" />
        </el-form-item>
        <el-form-item label="收货人名称" prop="consigneeName">
          <el-input v-model="apConfirmFormData.consigneeName" placeholder="请输入收货人名称" />
        </el-form-item>
        <el-form-item label="送货地址" prop="consigneeAddress">
          <el-input v-model="apConfirmFormData.consigneeAddress" placeholder="请输入送货地址" />
        </el-form-item>
        <el-form-item label="收货人电话" prop="consgneePhone">
          <el-input v-model="apConfirmFormData.consgneePhone" placeholder="请输入收货人电话" />
        </el-form-item>
        <el-form-item label="订单来源" prop="orderSource">
          <el-input v-model="apConfirmFormData.orderSource" placeholder="请输入订单来源" />
        </el-form-item>
        <el-form-item label="订单数量" prop="wtCnt">
          <el-input v-model="apConfirmFormData.wtCnt" placeholder="请输入订单数量" />
        </el-form-item>
        <el-form-item label="供应商ID" prop="supplierNo">
          <el-input v-model="apConfirmFormData.supplierNo" placeholder="请输入供应商ID" />
        </el-form-item>
        <el-form-item label="供应商名称" prop="supplierName">
          <el-input v-model="apConfirmFormData.supplierName" placeholder="请输入供应商名称" />
        </el-form-item>
        <el-form-item label="清关时间" prop="customClearanceDate">
          <el-input v-model="apConfirmFormData.customClearanceDate" placeholder="请输入清关时间" />
        </el-form-item>
        <el-form-item label="提单人" prop="processPersonName">
          <el-input v-model="apConfirmFormData.processPersonName" placeholder="请输入提单人" />
        </el-form-item>
        <el-form-item label="付款时间" prop="paymentDate">
          <el-input v-model="apConfirmFormData.paymentDate" placeholder="请输入付款时间" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="apConfirmFormData.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmApConfirmFormDialog">确 定</el-button>
        <el-button @click="resetApConfirmFormDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import ApConfirmApi from '@/api/accounting/ApConfirmApi'

const defaultApConfirmFormData = {
  customerCode: null,
  customerName: null,
  amount: null,
  adjustAmount: null,
  basicNo: null,
  slipNo: null,
  unionName: null,
  consigneeName: null,
  consigneeAddress: null,
  consgneePhone: null,
  orderSource: null,
  wtCnt: null,
  supplierNo: null,
  supplierName: null,
  orderStatus: '0',
  customClearanceDate: null,
  processPersonName: null,
  paymentDate: null
}
export default {
  name: 'ApConfirmList',
  data() {
    return {
      options: {
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        customerCode: null,
        customerName: null,
        amount: null,
        adjustAmount: null,
        basicNo: null,
        slipNo: null,
        unionName: null,
        consigneeName: null,
        consigneeAddress: null,
        consgneePhone: null,
        orderSource: null,
        wtCnt: null,
        supplierNo: null,
        supplierName: null,
        orderStatus: null,
        customClearanceDate: null,
        processPersonName: null,
        paymentDate: null,
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      apConfirmFormDialog: {
        title: '',
        visible: false
      },
      apConfirmFormData: Object.assign({}, defaultApConfirmFormData),
      apConfirmFormRules: {
        customerCode: [{ required: true, message: '客户编码不能为空', trigger: 'blur' }],
        customerName: [{ required: true, message: '客户名称不能为空', trigger: 'blur' }]
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
    },
    handleAdd() {
      this.openApConfirmFormDialog()
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return ApConfirmApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      this.openApConfirmFormDialog(id)
    },
    handleQuery() {
      this.loading = true
      ApConfirmApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return ApConfirmApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.data)
      })
    },
    openApConfirmFormDialog(id) {
      this.apConfirmFormDialog.visible = true
      if (id) {
        this.apConfirmFormDialog.title = '财务应付对账，按订单的颗粒生成的流水号 为数据粒度，与需要付款的对象进行对账。-修改'
        ApConfirmApi.get(id).then(response => {
          this.apConfirmFormData = {
            ...this.apConfirmFormData,
            ...response.data
          }
        })
      } else {
        this.apConfirmFormDialog.title = '财务应付对账，按订单的颗粒生成的流水号 为数据粒度，与需要付款的对象进行对账。-添加'
      }
    },
    confirmApConfirmFormDialog() {
      this.$refs['apConfirmForm'].validate(valid => {
        if (valid) {
          if (this.apConfirmFormData.id) {
            ApConfirmApi.edit(this.apConfirmFormData).then(response => {
              this.$message.success('修改成功')
              this.resetApConfirmFormDialog()
              this.handleQuery()
            })
          } else {
            ApConfirmApi.add(this.apConfirmFormData).then(response => {
              this.$message.success('添加成功')
              this.resetApConfirmFormDialog()
              this.handleQuery()
            })
          }
        }
      })
    },
    resetApConfirmFormDialog() {
      this.apConfirmFormDialog = {
        title: '',
        visible: false
      }
      this.apConfirmFormData = Object.assign({}, defaultApConfirmFormData)
      this.resetForm('apConfirmForm')
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
